﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace TLD_GUI {
    public partial class MainForm : Form {

        private List<Bitmap> imagesToProcess;

        public MainForm() {
            InitializeComponent();
            imagesToProcess = new List<Bitmap>();
        }

        private void backgroundLPRWorker_DoWork(object sender, DoWorkEventArgs e) {
            //Calculate the amount of operations
            foreach (Bitmap b in imagesToProcess) {
                Bitmap c;

                //First calculate the amount of pictures
                int numberofPics = imagesToProcess.Count;
                int numberOfOperations = numberofPics * 5;
                int operationsDone = 1;
                int platesFoundCounter = 0;

                //Do localization
                TLD_LPR.AlgorithmBase.VisionAlgorithm fla = new TLD_LPR.Localization.FinalLocateAlgorithm("Team 3 - Crop");
                c = fla.DoAlgorithm(b);

                backgroundLPRWorker.ReportProgress((int)((operationsDone++ * 100) / numberOfOperations));
                if ((Bitmap)c == null) {
                    //Console.WriteLine("FAIL ON: " + imageFileNames[imagesToProcess.IndexOf(b)]);
                    continue;
                } else {
                    platesFoundCounter++;
                }

                c.Save("step1.jpg");

                // Do transformation
                TLD_LPR.AlgorithmBase.VisionAlgorithm rpw = new TLD_LPR.Transformation.RotatePlateWrapper("Team 10 - Rotatie");
                c = rpw.DoAlgorithm(c);
                backgroundLPRWorker.ReportProgress((int)((operationsDone++ * 100) / numberOfOperations));
                c.Save("step2.jpg");

                // Do lighting
                TLD_LPR.AlgorithmBase.VisionAlgorithm la = new TLD_LPR.Lighting.LightingAlgorithm("Team 7 - Lighting");
                c = la.DoAlgorithm(c);
                backgroundLPRWorker.ReportProgress((int)((operationsDone++ * 100) / numberOfOperations));
                c.Save("step3.jpg");

                // Do segmentation?  --- "Team 6 - Segmentation"
                backgroundLPRWorker.ReportProgress((int)((operationsDone++ * 100) / numberOfOperations));
                
                TLD_LPR.Segmentation.SegmentationAlgorithm sa = new TLD_LPR.Segmentation.SegmentationAlgorithm("Team 6 - Segmentation");
                sa.DoAlgorithm(c);


                // Do OCR
                TLD_LPR.AlgorithmBase.OCRAlgorithm ocr = new TLD_LPR.OCR.TestOCRAlgorithm("Team 9 - OCR Algoritme (Tjerk)");
                String charactersFound = "";
                foreach (Bitmap l in sa.BB) {
                    l.Save(@"Result" + sa.BB.IndexOf(l) + ".jpg");
                    charactersFound += ocr.DoOCRAndGetCandidate(l);
                }
                Console.WriteLine("License: " + charactersFound);
                backgroundLPRWorker.ReportProgress((int)((operationsDone++ * 100) / numberOfOperations));


            }
            backgroundLPRWorker.ReportProgress(100);
        }

        private void btnStart_Click(object sender, EventArgs e) {
            imagesToProcess.Add(new Bitmap(@"D:\School\blok7\to7\THO7_wk4_testset\license_plate_9.jpg"));
            //imagesToProcess.Add(new Bitmap(@"D:\School\blok7\to7\THO7_wk4_testset\license_plate_2.jpg"));
            //imagesToProcess.Add(new Bitmap(@"D:\School\blok7\to7\THO7_wk4_testset\license_plate_8.jpg"));
            btnStart.Enabled = false;
            backgroundLPRWorker.RunWorkerAsync();
        }

        private void backgroundLPRWorker_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e) {
            btnStart.Enabled = true;
        }
    }
}
